home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Documentation / Books / Learn Java on the Macintosh / Learn Java Projects / 15.01 - threads / SimpleDraw.java < prev    next >
Text File  |  1996-04-24  |  5KB  |  188 lines

  1. /* -------------------------------------------------------------
  2. This applet paints a circle or square of the color you've chosen
  3. wherever you click. Every second, it blinks the shape to yellow.
  4. All shapes blink independently of each other. 
  5.  
  6. This applet keeps a list of the shapes you've drawn
  7. and paints all the shapes in the list when it repaints.
  8.  
  9. Java's classes: Applet    (applet)
  10.                 Event     (awt)     user-generated action
  11.                 Graphics  (awt)     used for drawing
  12.                 Color     (awt)     defines colors
  13.                 Choice    (awt)     shape and color selection choices
  14.                 Vector    (util)    list of shapes
  15.                 Thread    (lang)
  16.  
  17. Custom classes: SimpleDraw
  18.                 Circle              defines and draws circles
  19.                 Square              defines and draws squares
  20.                 Shape                a common ancestor for circles and squares
  21.                 BlinkThread         controls drawing for a shape
  22.  
  23. ------------------------------------------------------------- */
  24.  
  25. import java.applet.Applet;
  26. import java.util.*;
  27. import java.awt.*;
  28.  
  29. public class SimpleDraw extends Applet {
  30.    Vector  threads;
  31.    Choice  shapeChoice;
  32.    Choice  colorChoice;
  33.    
  34.    /** Create the GUI. */
  35.    public void init() {
  36.       threads = new Vector();
  37.       
  38.       shapeChoice = new Choice();
  39.       shapeChoice.addItem("Circle");
  40.       shapeChoice.addItem("Square");
  41.       add(shapeChoice);
  42.       
  43.       colorChoice = new Choice();
  44.       colorChoice.addItem("Red");
  45.       colorChoice.addItem("Green");
  46.       colorChoice.addItem("Blue");
  47.       add(colorChoice);
  48.       
  49.       BlinkThread.g = getGraphics(); // Get the graphics object for the applet
  50.    }
  51.    
  52.    /** Create a new shape. */
  53.    public boolean mouseUp(Event e, int x, int y) {
  54.    
  55.       BlinkThread t;
  56.       Shape s;  // This shape will be either a circle or a square.
  57.    
  58.       String shapeString = shapeChoice.getSelectedItem();
  59.       String colorString = colorChoice.getSelectedItem();
  60.       
  61.       if (shapeString.equals("Circle"))
  62.          s = new Circle();
  63.       else
  64.          s = new Square();
  65.       
  66.       if (colorString.equals("Red"))
  67.          s.color = Color.red;
  68.       else if (colorString.equals("Green"))
  69.          s.color = Color.green;
  70.       else
  71.          s.color = Color.blue;
  72.          
  73.       s.x = x;
  74.       s.y = y;
  75.       
  76.       t = new BlinkThread(s);
  77.       t.start();
  78.       threads.addElement(t);
  79.       
  80.       return true;
  81.    }
  82.  
  83.    /** Resume all the threads when the applet starts. */
  84.    public void start() {
  85.       BlinkThread t;
  86.       int         numThreads;
  87.       
  88.       numThreads = threads.size();
  89.       for (int i = 0; i < numThreads; i++) {
  90.       
  91.          t = (BlinkThread)threads.elementAt(i);         
  92.          t.resume();
  93.       }   
  94.    }      
  95.  
  96.    /** Suspend all the threads when the applet stops. */
  97.    public void stop() {
  98.       BlinkThread t;
  99.       int         numThreads;
  100.       
  101.       numThreads = threads.size();
  102.       for (int i = 0; i < numThreads; i++) {
  103.       
  104.          t = (BlinkThread)threads.elementAt(i);         
  105.          t.suspend();
  106.       }   
  107.    }      
  108.    
  109.    /** Stop all the threads when the applet goes away. */
  110.    public void destroy() {
  111.       BlinkThread t;
  112.       int         numThreads;
  113.       
  114.       numThreads = threads.size();
  115.       for (int i = 0; i < numThreads; i++) {
  116.       
  117.          t = (BlinkThread)threads.elementAt(i);         
  118.          t.stop();
  119.       }   
  120.    }    
  121. }
  122.  
  123. /** Shapes provide common characteristics for the circle and square. */
  124. abstract class Shape {
  125.    static public final int shapeRadius = 20;
  126.    
  127.    Color color;
  128.    int x;
  129.    int y;
  130.    
  131.    abstract void draw(Graphics g);
  132.    abstract void drawBlink(Graphics g);
  133. }
  134.  
  135. /** Draws and maintains circle information. */
  136. class Circle extends Shape {
  137.    void drawBlink(Graphics g) {
  138.       g.setColor(Color.yellow);
  139.       g.fillOval(this.x - shapeRadius, this.y - shapeRadius, shapeRadius * 2, shapeRadius * 2);
  140.    }
  141.    
  142.    void draw(Graphics g) {
  143.       g.setColor(this.color);
  144.       g.fillOval(this.x - shapeRadius, this.y - shapeRadius, shapeRadius * 2, shapeRadius * 2);
  145.    }
  146. }
  147.  
  148. /** Draws and maintains square information. */
  149. class Square extends Shape{
  150.    void drawBlink(Graphics g) {
  151.       g.setColor(Color.yellow);
  152.       g.fillRect(this.x - shapeRadius, this.y - shapeRadius, shapeRadius * 2, shapeRadius * 2);  
  153.    }
  154.    
  155.    void draw(Graphics g) {
  156.       g.setColor(this.color);
  157.       g.fillRect(this.x - shapeRadius, this.y - shapeRadius, shapeRadius * 2, shapeRadius * 2);  
  158.    }
  159. }
  160.  
  161. /** Thread to control when to blink a shape. */
  162. class BlinkThread extends Thread {
  163.    static Graphics g;
  164.    Shape s;
  165.    
  166.    BlinkThread(Shape s) {
  167.       this.s = s;
  168.    }
  169.    
  170.    public void run() {
  171.    
  172.       // don't ever exit the thread
  173.       while(true) {
  174.          
  175.          try {
  176.             s.drawBlink(g);
  177.             sleep(250);  // Go to sleep for a 1/4 of a second (250 milliseconds)
  178.             
  179.             s.draw(g);
  180.             sleep(1000); // Go to sleep for 1 second (1000 milliseconds)
  181.  
  182.          } catch (Exception e) {
  183.          }
  184.                   
  185.       }  
  186.    }
  187. }
  188.